import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import request.ClientRequestKind;
import socket.SocketInput;
import socket.SocketOutput;

public class Main {

	public static void main(String[] args) throws IOException {
		Config config = new Config();
		config.load();
		ServerSocket server = new ServerSocket(config.getPort());

		Socket socket = server.accept();
		SocketInput input = new SocketInput(socket.getInputStream());
		if (ClientRequestKind.RUN.ordinal() == input.readInt()) {
			new Updater(config.getOutput(), new SocketOutput(socket.getOutputStream()));
			antExecute();
		}
	}

	private static void antExecute() throws IOException {
		Runtime runtime = Runtime.getRuntime();
		Process process = runtime.exec("C:/Apps/apache-ant-1.8.4-bin/apache-ant-1.8.4/bin/ant -buildfile C:/Users/renato.lorenzi/Desktop/6_semestre/DUnitServer/src/test/ant/build.xml");
		BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
		// TODO Continuar isso. Ler a saida do ant e verificar se tem as tags
		// esperadas de <INFO> <ERROR> ou <WARNNING> .
		// log = new FileOutputStream(new File(""))
	}
}
